package com.example.bookdemo.config;

import com.example.bookdemo.enums.ResultEnum;
import com.example.bookdemo.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

@ControllerAdvice
@ResponseBody
@Slf4j
@RestControllerAdvice//代替上面两个注解
public class ErrAdvice {
    @ExceptionHandler
    public Result handler(Exception e){
        log.info("发生异常,e:{}",e);
        return Result.fail(ResultEnum.ELSE.getCode(),"发生异常:"+e.getMessage());
    }
    @ExceptionHandler
    public Result handler(NullPointerException e){
        log.info("发生NullPointerException,e:{}",e);
        return Result.fail(ResultEnum.NULL_PARAM.getCode(),"发生空指针异常:"+e.getMessage());
    }

    @ExceptionHandler(ArithmeticException.class)
    public Result handler(IllegalArgumentException e){
        log.info("发生IllegalArgumentException,e:{}",e);
        return Result.fail(ResultEnum.NULL_PARAM.getCode(),"参数为空");
    }

}
